package com.xingfly.repository;

import com.xingfly.model.Post;
import com.xingfly.model.User;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

/**
 * Created by SuperS on 2016/12/7.
 */
public interface PostRepository extends JpaRepository<Post, Long> {
    //查询id列表中的post
    @Query("SELECT p FROM Post p WHERE p.id in :ids")
    List<Post> findByIds(@Param("ids") List<Long> ids);

    //根据用户查找post 支持分页
    List<Post> findByAuthorId(Long userId, Pageable pageable);

    //TODO 根据用户查找post列表 并按照创建时间排序 支持分页
    @Query("SELECT p FROM Post p WHERE p.author.id=:id order by p.author.id ASC")
    List<Post> findByAuthorIdOrderByCreateTime(@Param("id") Long userId, Pageable pageable);

    //查询作者根据PostID
    @Query("SELECT p.author FROM Post p WHERE p.id=:id")
    User findAuthorByPostId(@Param("id") Long postId);

    //查询某个用户的Post数量
    @Query("SELECT COUNT(p) FROM Post p WHERE p.author.id = :id")
    Long findPostCountByAuthorId(@Param("id") Long userId);

    List<Post> findByAuthorId(Long userId);
}
